package com.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.ZuoyepingfenEntity;
import com.entity.view.ZuoyepingfenView;

import com.service.ZuoyepingfenService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.CommonUtil;


/**
 * 作业评分
 * 后端接口
 */
@RestController
@RequestMapping("/zuoyepingfen")
public class ZuoyepingfenController {
    @Autowired
    private ZuoyepingfenService zuoyepingfenService;

    /**
     * 后端列表
     */
	@RequestMapping("/page")
	public R page(@RequestParam Map<String, Object> params,ZuoyepingfenEntity zuoyepingfen, HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("jiaoshi")) {
			zuoyepingfen.setJiaoshigonghao((String)request.getSession().getAttribute("username"));
		}
		if(tableName.equals("xuesheng")) {
			zuoyepingfen.setXuehao((String)request.getSession().getAttribute("username"));
		}
		EntityWrapper<ZuoyepingfenEntity> ew = new EntityWrapper<ZuoyepingfenEntity>();

		// 添加作业类型筛选条件
		if(zuoyepingfen.getZuoyeleixing() != null && !"".equals(zuoyepingfen.getZuoyeleixing().trim())) {
			ew.eq("zuoyeleixing",zuoyepingfen.getZuoyeleixing());
		}

		// 添加班级筛选条件
		if (zuoyepingfen.getBanji() != null && !"".equals(zuoyepingfen.getBanji().trim())) {
			ew.eq("banji", zuoyepingfen.getBanji());
		}

		// 添加课程名称筛选条件
		if (zuoyepingfen.getKechengmingcheng() != null && !"".equals(zuoyepingfen.getKechengmingcheng().trim())) {
			ew.eq("kechengmingcheng", zuoyepingfen.getKechengmingcheng());
		}

		PageUtils page = zuoyepingfenService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zuoyepingfen), params), params));

		return R.ok().put("data", page);
	}

//	/**
//	 * 获取所有班级列表
//	 */
//	@RequestMapping("/getClasses")
//	public R getClasses(){
//		List<String> classes = zuoyepingfenService.selectDistinctClasses();
//		return R.ok().put("data", classes);
//	}
//
//	/**
//	 * 获取所有课程列表
//	 */
//	@RequestMapping("/getCourses")
//	public R getCourses(){
//		List<String> courses = zuoyepingfenService.selectDistinctCourses();
//		return R.ok().put("data", courses);
//	}
    
    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,ZuoyepingfenEntity zuoyepingfen, HttpServletRequest request){
        EntityWrapper<ZuoyepingfenEntity> ew = new EntityWrapper<ZuoyepingfenEntity>();
		PageUtils page = zuoyepingfenService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zuoyepingfen), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( ZuoyepingfenEntity zuoyepingfen){
       	EntityWrapper<ZuoyepingfenEntity> ew = new EntityWrapper<ZuoyepingfenEntity>();
      	ew.allEq(MPUtil.allEQMapPre( zuoyepingfen, "zuoyepingfen")); 
        return R.ok().put("data", zuoyepingfenService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(ZuoyepingfenEntity zuoyepingfen){
        EntityWrapper< ZuoyepingfenEntity> ew = new EntityWrapper< ZuoyepingfenEntity>();
 		ew.allEq(MPUtil.allEQMapPre( zuoyepingfen, "zuoyepingfen")); 
		ZuoyepingfenView zuoyepingfenView =  zuoyepingfenService.selectView(ew);
		return R.ok("查询作业评分成功").put("data", zuoyepingfenView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        System.out.println("查询详情，ID: " + id);
        ZuoyepingfenEntity zuoyepingfen = zuoyepingfenService.selectById(id);
        System.out.println("查询结果: " + zuoyepingfen);
        return R.ok().put("data", zuoyepingfen);
    }

    /**
     * 前端详情
     */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        ZuoyepingfenEntity zuoyepingfen = zuoyepingfenService.selectById(id);
        return R.ok().put("data", zuoyepingfen);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ZuoyepingfenEntity zuoyepingfen, HttpServletRequest request){
    	System.out.println("接收到保存请求，数据: " + zuoyepingfen.toString());
    	zuoyepingfen.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(zuoyepingfen);
        try {
            zuoyepingfenService.insert(zuoyepingfen);
            System.out.println("保存成功，ID: " + zuoyepingfen.getId());
            return R.ok();
        } catch (Exception e) {
            System.err.println("保存失败: " + e.getMessage());
            e.printStackTrace();
            return R.error("保存失败: " + e.getMessage());
        }
    }
    
    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody ZuoyepingfenEntity zuoyepingfen, HttpServletRequest request){
    	zuoyepingfen.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(zuoyepingfen);
        zuoyepingfenService.insert(zuoyepingfen);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ZuoyepingfenEntity zuoyepingfen, HttpServletRequest request){
        System.out.println("接收到更新请求，数据: " + zuoyepingfen.toString());
        //ValidatorUtils.validateEntity(zuoyepingfen);
        try {
            zuoyepingfenService.updateById(zuoyepingfen);//全部更新
            System.out.println("更新成功，ID: " + zuoyepingfen.getId());
            return R.ok();
        } catch (Exception e) {
            System.err.println("更新失败: " + e.getMessage());
            e.printStackTrace();
            return R.error("更新失败: " + e.getMessage());
        }
    }
    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        zuoyepingfenService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
    /**
     * 提醒接口
     */
	@RequestMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, 
						 @PathVariable("type") String type,@RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);
		
		if(type.equals("2")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart")!=null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date()); 
				c.add(Calendar.DAY_OF_MONTH,remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend")!=null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH,remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}
		
		Wrapper<ZuoyepingfenEntity> wrapper = new EntityWrapper<ZuoyepingfenEntity>();
		if(map.get("remindstart")!=null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend")!=null) {
			wrapper.le(columnName, map.get("remindend"));
		}

		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("jiaoshi")) {
			wrapper.eq("jiaoshigonghao", (String)request.getSession().getAttribute("username"));
		}
		if(tableName.equals("xuesheng")) {
			wrapper.eq("xuehao", (String)request.getSession().getAttribute("username"));
		}

		int count = zuoyepingfenService.selectCount(wrapper);
		return R.ok().put("count", count);
	}

	/**
	 * 成绩统计分析
	 */
	@RequestMapping("/group/pingfen")
	public R groupPingfen(HttpServletRequest request) {
		EntityWrapper<ZuoyepingfenEntity> ew = new EntityWrapper<ZuoyepingfenEntity>();

		// 处理权限 (与page方法相同的权限逻辑)
		String tableName = request.getSession().getAttribute("tableName").toString();
		if (tableName.equals("jiaoshi")) {
			ew.eq("jiaoshigonghao", request.getSession().getAttribute("username"));
		}
		if (tableName.equals("xuesheng")) {
			ew.eq("xuehao", request.getSession().getAttribute("username"));
		}

		// 添加提交类型筛选条件
		String zuoyeleixing = request.getParameter("zuoyeleixing");
		if (StringUtils.isNotBlank(zuoyeleixing)) {
			ew.eq("zuoyeleixing", zuoyeleixing);
		}

//		// 添加班级筛选条件
//		String banji = request.getParameter("banji");
//		if (StringUtils.isNotBlank(banji)) {
//			ew.eq("banji", banji);
//		}

		// 只查询评分字段
		ew.setSqlSelect("pingfen");

		List<Map<String, Object>> list = zuoyepingfenService.selectMaps(ew);

		return R.ok().put("data", list);
	}

}
